package com.dy.lock.screen.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.dy.lock.screen.constants.Constant;
import com.dy.lock.screen.dao.CommonCommentDao;
import com.dy.lock.screen.dao.MaterialCommentDao;
import com.dy.lock.screen.pojo.CommonCommentInfo;
import com.dy.lock.screen.pojo.MaterialCommentInfo;
import com.dy.lock.screen.service.MaterialCommentService;
import com.dy.lock.screen.utils.CommonUtil;

@Service("materialCommentService")
public class MaterialCommentServiceImpl implements MaterialCommentService {

	@Resource(name = "materialCommentDao")
	private MaterialCommentDao materialCommentDao;
	@Resource(name = "commonCommentDao")
	private CommonCommentDao commonCommentDao;

	@Override
	public List<MaterialCommentInfo> queryPage(Map<String, Object> para) {
		List<MaterialCommentInfo> list = this.materialCommentDao.queryT(para);
		return list;
	}

	@Override
	public MaterialCommentInfo queryByPrimaryKey(String mid) {
		return this.materialCommentDao.queryByPrimaryKey(mid);

	}

	@Override
	public Integer queryCount(Map<String, Object> para) {
		return this.materialCommentDao.queryCount(para);
	}

	@Override
	public int save(MaterialCommentInfo mcInfo) {
		mcInfo.setCommentId((CommonUtil.createRandomNum()));
		for (int i = 0; i < 20; i++) {
			MaterialCommentInfo m = this.materialCommentDao
					.queryByPrimaryKey(mcInfo.getCommentId());
			if (m != null) {
				mcInfo.setCommentId((CommonUtil.createRandomNum()));
			} else {
				break;
			}
		}
		mcInfo.setCommentTime(new Date());
		mcInfo.setIsTop(Constant.NOT_TOPED);
		mcInfo.setIsDelete(Constant.IS_NOT_DELETE);
		int re = this.materialCommentDao.insert(mcInfo);
		return re;
	}

	@Override
	public MaterialCommentInfo queryCommentDetail(Map<String, Object> para) {
		// TODO Auto-generated method stub
		return this.materialCommentDao.queryCommentDetail(para);
	}

	@Override
	public int falseDelete(MaterialCommentInfo maComentInfo) {
		int re = this.materialCommentDao.falseDelete(maComentInfo);
		int result = -1;
		if (re > 0) {
			CommonCommentInfo ccInfo = new CommonCommentInfo();
			ccInfo.setUserId(maComentInfo.getUserId());
			ccInfo.setCommentId(maComentInfo.getCommentId());
			ccInfo.setIsDelete(Constant.IS_DELETE);
			result = this.commonCommentDao.falseDelete(ccInfo);
		}
		return result;
	}
}
